<template>
  <div class="user-menu">
    <!-- 未登录状态 -->
    <div v-if="!userStore.isLoggedIn" class="login-actions">
      <el-button type="primary" size="small" @click="goToLogin">
        登录
      </el-button>
    </div>

    <!-- 已登录状态 -->
    <el-dropdown v-else trigger="click" @command="handleCommand">
      <div class="user-avatar-container">
        <el-avatar
          :size="32"
          :src="userStore.userInfo?.avatar"
          class="user-avatar"
        >
          <el-icon><User /></el-icon>
        </el-avatar>
        <span class="username">{{ userStore.userInfo?.nickname || userStore.userInfo?.username }}</span>
        <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
      </div>

      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="profile" :icon="User">
            个人资料
          </el-dropdown-item>
          <el-dropdown-item command="settings" :icon="Setting">
            设置
          </el-dropdown-item>
          <el-dropdown-item divided command="logout" :icon="SwitchButton">
            退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, Setting, SwitchButton, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '@/shared/user.ts'

const router = useRouter()
const userStore = useUserStore()

// 前往登录页
const goToLogin = () => {
  router.push('/login')
}

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中')
      break
    case 'settings':
      ElMessage.info('设置功能开发中')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '确认退出',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    userStore.logout()
    // 如果当前页面需要登录权限，跳转到登录页
    if (router.currentRoute.value.meta?.requiresAuth) {
      router.push('/login')
    }
  }).catch(() => {
    // 用户取消退出
  })
}
</script>

<style scoped>
.user-menu {
  display: flex;
  align-items: center;
  height: 32px;
}

.login-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 100%;
}

.user-avatar-container {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s;
  height: 32px;
  box-sizing: border-box;
}

.user-avatar-container:hover {
  background-color: var(--el-fill-color-light);
}

.user-avatar {
  flex-shrink: 0;
}

.username {
  font-size: 14px;
  color: var(--el-text-color-primary);
  font-weight: 500;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-icon {
  font-size: 12px;
  color: var(--el-text-color-regular);
  transition: transform 0.2s;
}

.user-avatar-container:hover .dropdown-icon {
  transform: rotate(180deg);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .username {
    display: none;
  }
  
  .user-avatar-container {
    padding: 4px;
  }
}
</style>